const app = getApp()
const md5 = require('md5');
Page({
    /**
     * 页面的初始数据
     */
    data: {
        showPayImg: true,
        fee: 0.01,
        id: null,
        timeLeft: '',
        datetimeTo: 300,
        inter: '',
        timer: '',
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        const that = this
        // const fee = options.fee+".00";
        const fee = "0.01";
        const id = options.id;
        this.setData({
            fee: fee,
            id: id,
        })
        if (!fee || !id) {
            wx.showToast({
                title: '系统出错，请稍后重试',
                icon: 'none',
                duration: 2000
            })
            wx.navigateBack({})
        } else {
            wx.request({
                url: 'https://gateway.xddpay.com?format=json',
                data: {
                    order_no: id, pay_type: 44, money: fee, app_id: 12148,
                    sign: md5('order_no=' + id + '&subject=&pay_type=44&money=' + fee + '&app_id=12148&extra=&a59b63c420fb490db6108597d39401ea').toUpperCase()
                },
                dataType: 'json',
                method: 'POST',
                header: {
                    'custom-header': 'POST',
                    'content-type': 'application/x-www-form-urlencoded',
                },
                success: (res) => {
                    if (res.data.qr_img != null) {
                        that.setData({
                            payRes: res.data,
                            showPayImg: true,
                        })
                    }
                }
            })
        }
    },
    onShow: function () {
        const that = this
        that.data.timer = setInterval(() => {
            const temp = that.data.datetimeTo - 1;
            that.setData({
                datetimeTo: temp
            });
            if (temp > 0) {
                const a = parseInt(temp / 60)
                const b = temp % 60
                if (a > 0) {
                    that.setData({
                        timeLeft: a + '分' + b + '秒'
                    });
                } else {
                    that.setData({
                        timeLeft: b + '秒'
                    });
                }
            } else {
                wx.showToast({
                    title: '订单关闭',
                    icon: 'none',
                    duration: 2000,
                    success: function () {
                        clearInterval(that.data.timer);
                        clearInterval(that.data.inter);
                        setTimeout(function () {
                            wx.navigateBack({})
                        }, 2000);
                    }
                })
            }
        }, 1000);
        that.data.inter = setInterval(
            function () {
                wx.request({
                    url: app.globalData.baseurl + "pay/pay",
                    data: {id: that.data.id},
                    success: function (res) {
                        if (res.data.code === 200) {
                            wx.showToast({
                                title: '支付成功',
                                icon: 'success',
                                duration: 2000,
                                success: function () {
                                    clearInterval(that.data.timer);
                                    clearInterval(that.data.inter);
                                    setTimeout(function () {
                                        wx.navigateBack({})
                                    }, 2000);
                                }
                            })
                        }
                    },
                    method: "POST"
                })
            }, 3500);
    },
})
